package com.huitone.gddw.controller.analysis.traffic;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huitone.gddw.common.excel.CreateExcel;
import com.huitone.gddw.controller.common.CommonController;
import com.huitone.gddw.dao.entity.RecvSm;
import com.huitone.gddw.dao.entity.SmLog;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.analysis.traffic.IUpFlowRepService;
import com.huitone.gddw.service.dto.analysis.traffic.UpSmRecDTO;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 上行短信明细
 * @author wuluodan
 *
 */
@Controller
@RequestMapping(value="upSmRec")
public class UpSmRecController extends CommonController {

	private final static Logger LOGGER = Logger.getLogger(UpSmRecController.class);
	
	@Autowired
	private IUpFlowRepService upFlowRepService;
	
	/**
	 * 查询
	 * @param page
	 * @param request
	 * @return QueryPage
	 */
	@ResponseBody
	@RequestMapping(value="query")
	public QueryPage<UpSmRecDTO> query(QueryPage<RecvSm> page, HttpServletRequest request) {
		super.logType = SmLog.LOGTYPE_SELECT;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]查询上行短信明细";
		QueryPage<UpSmRecDTO> result = null;
		try {
			page.setParams(getParams(request));
			result = upFlowRepService.selectDetial(page);
		} catch (Exception e) {
			LOGGER.error("查询上行短信明细出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return result;
	}
	
	/**
	 * 获取参数信息
	 * @param request
	 * @return Map<String, Object>
	 */
	private Map<String, Object> getParams(HttpServletRequest request) {
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		String svrNode = request.getParameter("svrNode");
		String gateLinkId = request.getParameter("gateLinkId");
		String smContent = request.getParameter("smContent");
		
		String upTotal = request.getParameter("upTotal");
		String upNone = request.getParameter("upNone");
		String upSuc = request.getParameter("upSuc");
		String upFail = request.getParameter("upFail");
		String mobileCorp = request.getParameter("mobileCorp");
		String dateFormat = request.getParameter("dateFormat");
		
		String userNo = request.getParameter("userNo");
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("svrNode", svrNode);
		params.put("gateLinkId", gateLinkId);
		params.put("smContent", smContent);
		
		params.put("upTotal", upTotal);
		params.put("upNone", upNone);
		params.put("upSuc", upSuc);
		params.put("upFail", upFail);
		params.put("mobileCorp", mobileCorp);
		params.put("dateFormat", dateFormat);
		
		params.put("userNo", userNo);
		return params;
	}
	
	/**
	 * 导出 Excel
	 * @param request
	 * @param response
	 */
	@RequestMapping(value="/out-excel", method=RequestMethod.POST)
	public void outExcel(HttpServletRequest request, HttpServletResponse response) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String fileName = "上行短信明细表";
		
		Map<String, Object> params = new HashMap<String, Object>();
		for (Object name : request.getParameterMap().keySet()) {
			params.put(name.toString(), request.getParameter(name.toString()));
		}
		
		List<UpSmRecDTO> list = upFlowRepService.getRecordDetial(params);
		if (null == list || 0 == list.size()) {
			list.add(new UpSmRecDTO());
		}
		
		HSSFWorkbook wb = (HSSFWorkbook)CreateExcel
				.getInstance()
				.CreateNewExcelNoTemplate(fileName, list).getWorkbook();
		OutputStream fOut = null;
		try {
			fileName = new String(fileName.getBytes("gbk"), "ISO8859-1");
			response.reset();
			response.setContentType("application/octet-stream; charset=utf-8");
			response.setHeader("Content-Disposition",
					"attachment;filename="+fileName+sdf.format(new Date())+".xls");
			fOut = response.getOutputStream();
			wb.write(fOut);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fOut.flush();
				fOut.close();
			} catch (IOException e) {

			}
		}
	}
	
}
